var login = new Vue({
    el: '#app-login',
    data: {
        user: {
            gender: "",
            phone: "",
            profile: "",
            nickname: ""
        },
        //错误提示
        showAlert: false,
        showAlert_vft: false,
        alert_p: "手机号或密码错误",
        alert_vft: "手机号或验证码",

        //登录输入信息
        phone: '',
        phone_vft: '',
        password: '',
        vft_sms: '',
        btn_vft: "获取验证码",
        rememberPwd: true,
        count: 60
    },
    methods: {
        login: function (type, phone, password) {
            console.log("phone:" + phone + ",pwd:" + password);
            this.$http.post("http://120.77.223.88:8080/red/Login", {
                type: type,
                phone: phone,
                password: password
            }, {emulateJSON: true})
                .then((res) => {
                    console.log(res.data);
                    if (res.data.status == 1) {
                        login.user = res.data.data[0];
                        //存储Cookies
                        window.localStorage.setItem('isLogin', true);
                        window.localStorage.setItem('nickname', login.user.nickname);
                        window.localStorage.setItem('phone', login.user.phone);
                        //返回
                        window.history.back(-1);
                        //刷新
                        alert("true");
                        window.location.reload(true);
                    } else {
                        console.log("登录失败");
                        alert("登录失败");
                    }
                }, (res) => {
                    alert("登录失败: " + res);
                });
        },

        //获取验证码
        getSMSCode: function () {
            var btn_sms = document.getElementById("btnSMS");
            var t;
            if (this.checkPhone_S()) {
                if (this.count === 60) {
                    Bmob.initialize("8fede9808606d9e527239509b616fcfas", "6d5b70c5d7f464beca9fda0ccd5dc89b");
                    Bmob.Sms.requestSmsCode({"mobilePhoneNumber": this.phone_vft, "template": "T1"}).then((obj) => {
                        btn_sms.setAttribute("style", "color:#aaa");
                        t = setInterval(function () {
                            console.log(login.count);
                            login.btn_vft = "获取验证码(" + login.count + ")";
                            login.count--;
                            if (login.count <= 0) {
                                clearInterval(t);
                                login.count = 60;
                                login.btn_vft = "获取验证码";
                                btn_sms.setAttribute("style", "color:#f6007d");
                            }
                        }, 1000);
                    }, (err) => {
                        console.log("获取验证码失败:"+err);
                        login.showAlert_S("获取验证码失败");
                    });
                }
            } else {
                console.log("判断出错");
                this.showAlert_S("手机号错误");
            }
        },

        //密码登陆
        login_p: function () {
            if(this.checkPhone_P()){
                console.log("login in with password...");
                //访问服务器
                console.log(this.password);
                this.login(0, this.phone, this.password);
                if (this.rememberPwd)
                    window.localStorage.setItem('password', this.password);
                else
                    window.localStorage.setItem('password', "");
            }else{
                this.showAlert_P("手机号错误");
            }
        },

        // 验证码登陆
        login_sms: function () {
            var phone = this.phone_vft;
            if (this.checkPhone_S() && this.checkVer()) {
                //验证密码
                Bmob.Sms.verifySmsCode(phone, this.vft_sms).then(function (obj) {
                    console.log("验证成功");
                    //访问服务器
                    login.login(1, phone, null);
                }, function (err) {
                    console.log(err.toString());
                    this.showAlert_P("验证码错误");
                });
            }
        },

        //动态检测输入是否正确
        checkPhone_P: function () {
            if (!(/^[1][3,4,5,7,8][0-9]{9}$/.test(this.phone))) {
                this.showAlert_P("手机号错误");
                $("#btn_login").attr("disabled", true);
                return false;
            } else {
                this.hideAlert_P();
                $("#btn_login").attr("disabled", false);
                if (window.localStorage.getItem('phone') === this.phone && window.localStorage.getItem('password') !== undefined) {
                    this.password = window.localStorage.getItem('password');
                }
                return true;
            }
        },

        checkPhone_S: function () {
            if (!(/^[1][3,4,5,7,8][0-9]{9}$/.test(this.phone_vft))) {
                this.showAlert_S("手机号错误");
                return false;
            } else {
                this.hideAlert_S();
                return true;
            }
        },

        checkVer: function () {
            if (this.vft_sms.length != 6) {
                this.showAlert_S("验证码错误");
                return false;
            } else {
                this.hideAlert_S();
                return true;
            }

        },

        // 显示隐藏错误信息
        showAlert_P: function (str) {
            this.alert_p = str;
            this.showAlert = true;
        },

        hideAlert_P: function () {
            this.showAlert = false;
        },

        showAlert_S: function (str) {
            this.alert_vft = str;
            this.showAlert_vft = true;
        },

        hideAlert_S: function () {
            this.showAlert_vft = false;
        }
    }
});

